<script setup>

import {Delete, Edit, Plus, Refresh, WarningFilled} from "@element-plus/icons-vue";
import {Search} from "@element-plus/icons-vue/global";
import {onMounted, reactive, ref} from "vue";
import {ElMessage} from "element-plus";
import {addStudent, deleteStudent, searchStudent, updateStudent} from "@/api/dormitoryApi/studentApi.js";

const dormId = ref('')
const inputStuNum = ref('')
const inputDormNum = ref('')
const inputArea = ref('')
const addFormVisible = ref(false)
const editFormVisible = ref(false)
const students = ref([])
const addState = ref('未入住')
const addPhone = ref('')
const editState = ref('')
const editPhone = ref('')
const currentPage=ref(1);
const total = ref(100);

const clear = () => {
  inputStuNum.value = '';
  inputDormNum.value = '';
  inputArea.value = '';
  search();
}

const options = [
  {
    value: '已入住',
    label: '已入住',
  },
  {
    value: '未入住',
    label: '未入住',
  }
]

const addForm = reactive({
  dormNum: '',
  stuNum: '',
  name: '',
  area: '',
})
const editForm = reactive({
  dormNum: '',
  stuNum: '',
  name: '',
  area: '',
})

const edit = (name,area,phone,state,dormStuId,dormNum,stuNum) => {
  editFormVisible.value = true;
  dormId.value = dormStuId;
  editForm.dormNum = dormNum;
  editForm.stuNum = stuNum;
  editForm.name = name;
  editForm.area = area;
  editState.value = state;
  editPhone.value = phone;
}

const search = () => {
  const searchParams = {
    limit: 10,
    page: currentPage.value,
    id: inputStuNum.value,
    num: inputDormNum.value,
    name: inputArea.value,
  };

  searchStudent(searchParams).then(res => {
    if(res.status === 200){
      //查询成功
      students.value = res.data.dormStuIdVoList;
      total.value = res.data.total;
    }
  }).catch(() => {
    ElMessage.error("查询失败");
  })
}

const add = () => {
  const addParams = {
    name: addForm.name,
    area: addForm.area,
    phone: addPhone.value,
    state: addState.value,
    dormNum: addForm.dormNum,
    stuNum: addForm.stuNum,
  };

  addStudent(addParams).then(res => {
    if(res.status === 200){
      //添加成功
      ElMessage.success("添加成功");
      addFormVisible.value = false;
      search();
    }
  }).catch(() => {
    ElMessage.error("添加失败");
  })
}

const update = (name,area,phone,state,dormStuId,dormNum,stuNum) => {
  const updateParams = {
    name: name,
    area: area,
    phone: phone,
    state: state,
    dormStuId: dormStuId,
    dormNum: dormNum,
    stuNum: stuNum,
  };

  updateStudent(updateParams).then(res => {
    if(res.status === 200){
      //修改成功
      ElMessage.success("修改成功");
      editFormVisible.value = false;
      search();
    }
  }).catch(() => {
    ElMessage.error("修改失败");
  })
}

const deleteMessage = (dormStuId,dormNum,area) => {
  const deleteParams = {
    dormStuId: dormStuId,
    dormNum: dormNum,
    area: area,
  };

  deleteStudent(deleteParams).then(res => {
    if(res.status === 200){
      //删除成功
      ElMessage.success("删除成功");
      search();
    }
  }).catch(() => {
    ElMessage.error("删除失败");
  })
}

const handleCurrentChange = (val) => {
  currentPage.value = val;
  search();
}

onMounted(() => {
  search();
})
</script>

<template>
  <div class="view-container">
    <div class="header">
      <el-card>
        <span style="font-size: large; color: rgb(51,133,217);">学生住宿信息管理</span>
        <div class="extra">
          <el-input v-model="inputStuNum" style="width: 180px" placeholder="输入学号" />
          <el-input v-model="inputDormNum" style="width: 180px" placeholder="输入宿舍号" />
          <el-input v-model="inputArea" style="width: 250px" placeholder="输入片区及楼栋名" />
          <el-button type="primary" plain @click="search"><el-icon><Search /></el-icon>搜索学生</el-button>
          <el-button @click="clear"><el-icon><Refresh /></el-icon>重置</el-button>
          <el-button type="primary" @click="addFormVisible = true"><el-icon><Plus /></el-icon>添加学生信息</el-button>
        </div>
      </el-card>
    </div>

    <el-dialog v-model="addFormVisible" title="添加学生信息" width="500">
      <el-form :model="addForm">
        <el-form-item label="宿舍号">
          <el-input v-model="addForm.dormNum" placeholder="请输入宿舍号" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学号">
          <el-input v-model="addForm.stuNum" placeholder="请输入学号" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="addForm.name" placeholder="请输入姓名" autocomplete="off" />
        </el-form-item>
        <el-form-item label="所属片区及楼栋">
          <el-input v-model="addForm.area" placeholder="请输入所属片区及楼栋" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="addState" placeholder="请选择状态">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="入住时间">
          <el-date-picker
              v-model="addPhone"
              type="date"
              placeholder="请选择入住时间"
              value-format="YYYY-MM-DD"
              style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addFormVisible = false">取消</el-button>
          <el-button type="primary" @click="add">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>

    <div class="table-container">
      <el-table :data="students" style="width: 100%">
        <el-table-column label="序号" prop="stuId" align="center" width="150"></el-table-column>
        <el-table-column label="宿舍号" prop="dormNum" align="center" width="100"></el-table-column>
        <el-table-column label="学号" prop="stuNum" align="center" width="150"></el-table-column>
        <el-table-column label="姓名" prop="name" align="center" width="150"></el-table-column>
        <el-table-column label="所属片区及楼栋" prop="area"></el-table-column>
        <el-table-column label="状态" prop="state" align="center" width="100"></el-table-column>
        <el-table-column label="入住时间" prop="phone" align="center" width="130"></el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template #default="{ row }">
            <el-button :icon="Edit" circle plain type="primary" @click="edit(row.name,row.area,row.phone,row.state,row.dormStuId,row.dormNum,row.stuNum)"></el-button>
            <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="WarningFilled"
                icon-color="#ffcc00"
                title="确定删除此条信息？"
                @confirm="deleteMessage(row.dormStuId,row.dormNum,row.area)"
            >
              <template #reference>
                <el-button :icon="Delete" circle plain type="danger"></el-button>
              </template>
            </el-popconfirm>
            <el-dialog v-model="editFormVisible" title="修改学生信息" width="500">
              <el-form :model="editForm">
                <el-form-item label="宿舍号">
                  <el-input v-model="editForm.dormNum" placeholder="请输入宿舍号" autocomplete="off" />
                </el-form-item>
                <el-form-item label="学号">
                  <el-input v-model="editForm.stuNum" placeholder="请输入学号" autocomplete="off" />
                </el-form-item>
                <el-form-item label="姓名">
                  <el-input v-model="editForm.name" placeholder="请输入姓名" autocomplete="off" />
                </el-form-item>
                <el-form-item label="所属片区及楼栋">
                  <el-input v-model="editForm.area" placeholder="请输入所属片区及楼栋" />
                </el-form-item>
                <el-form-item label="状态">
                  <el-select v-model="editState" placeholder="请选择状态">
                    <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item label="入住时间">
                  <el-date-picker
                      v-model="editPhone"
                      type="date"
                      placeholder="请选择入住时间"
                      value-format="YYYY-MM-DD"
                      style="width: 100%"
                  />
                </el-form-item>
              </el-form>
              <template #footer>
                <div class="dialog-footer">
                  <el-button @click="editFormVisible = false">取消</el-button>
                  <el-button type="primary" @click="update(editForm.name,editForm.area,editPhone,editState,dormId,editForm.dormNum,editForm.stuNum)">
                    确定
                  </el-button>
                </div>
              </template>
            </el-dialog>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="暂无数据" />
        </template>
      </el-table>
    </div>
    <div class="container-bottom">
      <el-pagination
          v-model:current-page="currentPage"
          page-size="10"
          :background="true"
          layout="total, prev, pager, next"
          :total="total"
          style="margin-top: 30px; justify-content: center"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style scoped>
.view-container{
  overflow: hidden;
  margin-top: 30px;
  margin-left: 300px;
  margin-right: 50px;
}
.table-container{
  margin-top: 20px;
}
.extra{
  margin-top: 20px;
  display: flex;
  gap: 30px;
}
</style>
